package cn.hxth.datasync;

import cn.hxth.datasync.redis.RedisObjectSerializer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.http.HttpHeaders;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.Charset;
import java.util.Base64;

@SpringBootApplication
public class HxthMqApplication {
	@Bean(name = "myRedisTemplate")
	public RedisTemplate<String, Object> getRedisTemplate(
			RedisConnectionFactory factory) {
		RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
		redisTemplate.setConnectionFactory(factory);
		redisTemplate.setKeySerializer(new StringRedisSerializer()); // key的序列化类型
		redisTemplate.setValueSerializer(new RedisObjectSerializer()); // value的序列化类型
		return redisTemplate;
	}
	@Bean
	public RestTemplate getRestTemplate(){
		RestTemplate restTemplate = new RestTemplate() ;
		/*HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType.parseMediaType("application/text; charset=UTF-8");
		JSONObject jsonObject = new JSONObject() ;
		headers.setContentType(type);
		jsonObject.put("mid","admin") ;
		jsonObject.put("password","hello") ;
		HttpEntity<String> httpEntity = new HttpEntity<>(jsonObject.toJSONString(),headers) ;
		restTemplate.postForObject("http://localhost:81/loginForm",httpEntity,String.class) ;*/
		return restTemplate ;
	}
	@Bean
	public HttpHeaders getHeaders() { // 要进行一个Http头信息配置
		HttpHeaders headers = new HttpHeaders(); // 定义一个HTTP的头信息
		String auth = "hxth:hello"; // 认证的原始信息
		byte[] encodedAuth = Base64.getEncoder()
				.encode(auth.getBytes(Charset.forName("US-ASCII"))); // 进行一个加密的处理
		// 在进行授权的头信息内容配置的时候加密的信息一定要与“Basic”之间有一个空格
		String authHeader = "Basic " + new String(encodedAuth);
		headers.set("Authorization", authHeader);
		return headers;
	}

	public static void main(String[] args) {
		SpringApplication.run(HxthMqApplication.class, args);
	}
}
